package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.pojo.dto.PictureAddNewDto;
import cn.tedu.csmall.product.pojo.entity.PmsCategory;
import cn.tedu.csmall.product.pojo.entity.PmsPicture;
import cn.tedu.csmall.product.pojo.vo.*;
import cn.tedu.csmall.product.service.PictureService;
import cn.tedu.csmall.product.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PictureServiceImpl implements PictureService {

    @Autowired
    PictureMapper mapper;
    @Override
    public void addNew(PictureAddNewDto addNewDto) {
        // 创建Category对象
        PmsPicture category = new PmsPicture();
        // 复制属性
        BeanUtils.copyProperties(addNewDto, category);
        // 调用Mapper对象的insert()方法执行插入
        int i = mapper.insert(category);
        if (i != 1) {
            String message = "添加照片失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        PictureStandardVo pictureStandardVo = mapper.selectById(id);
        if (pictureStandardVo == null) {
            String message = "该照片不存在";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        int i = mapper.deleteById(id);
        if (i != 1){
            String message = "删除不成功";
            throw new ServiceException(ServiceCode.ERROR_DELETE,message);
        }
    }
}
